/*
This file generates the tables related to the Long-Term Debt Channel of 
Monetary Policy:

	- Table 6: Lenders' funding growth response to changes in MP
	- Table 7: Nonbank lending and funding structure 

*/



	
/////////////////////////////////////////////////////////////////////////////////
/// Table 6: Lenders' funding growth response to changes in MP
/////////////////////////////////////////////////////////////////////////////////

use "${procdata}/final_Experian.dta", clear

/// Prepare data, labels and outcome vars
egen id = group(cvrnr_lender)
xtset id year

local fundingvars TotalEquity ShortTermDebt LongTermDebt LTfunding  

///  Regress funding growth on lagged MP shock, separately for banks and nonbanks
eststo clear
local macro_ctrls DK_real_gdp_grate DK_gdp_growth_forecast DK_inflation_rate  vix
	 
foreach lender in 0 1 {
	
	foreach outcome of local fundingvars  {

	 eststo `outcome'_`lender': qui reghdfe `outcome'_gr_w L.(JK_mpshocksign `macro_ctrls') ///
				if nonbank_lender == `lender', noconstant a(id) vce(cluster id)
 
	}	 
}	


		
/// Export results
qui estadd local LenderFE "Yes": *
qui estadd local MacroCtrl  "Yes": *

local stats_LenCl N r2 MacroCtrl  LenderFE  
local label_LenClust `""Observations" "R2" "Macro Controls" "Lender FE"  "'

local tab_style star(* 0.10 ** 0.05 *** 0.01) fragment keep(L.JK_mpshocksign) ///
		varwidth(25) collabels(none) cells(b(star fmt(2)) se(fmt(2) par)) ///
		varlabels(L.JK_mpshocksign "MP Shock") booktabs 
		
esttab TotalEqu*_0  Short*_0 Long*_0 LTfunding*_0   using "$tables/Experian_reg_funding.tex", ///
		prehead("{\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi} \begin{tabular}{l*{5}{c}} \toprule")  ///
		posthead("\hline \\ \multicolumn{4}{l}{\textbf{A. Banks}} \\\\[-1ex]") ///
		stats(N r2, labels("Observations" "R2") fmt(%12.0gc 2)) ///
		mtitles("Equity" "Short-term debt" "Long-term debt" "Long-term funding") ///
		`tab_style' replace

esttab TotalEqu*_1 Short*_1  Long*_1  LTfunding*_1  using "$tables/Experian_reg_funding.tex", ///
		posthead("\\ \multicolumn{4}{l}{\textbf{B. Nonbanks}} \\\\[-1ex]") ///
		append nomtitles nonumbers nolines prefoot("\hline") ///
		postfoot("\hline\hline \end{tabular} }")  `tab_style' ///
		stats(`stats_LenCl', labels(`label_LenClust') fmt(%12.0fc 2)) 
		


		
		
/////////////////////////////////////////////////////////////////////////////////
///
/// 					Nonbank Funding X Lending 
///
/////////////////////////////////////////////////////////////////////////////////
		
		
/////////////////////////////////////////////////////////////////////////////////
/// Table 7.a: Nonbank corporate lending and funding structure 
/////////////////////////////////////////////////////////////////////////////////


// Load corporate credit data FOR NONBANKS ONLY, and merge with lenders' balance sheets
use if delinquency != 1 & nonbank_lender == 1 using "${procdata}/URTEVIRK_FIRE_FIRM_03-18.dta", clear 
merge m:1 cvrnr_lender year using "$procdata/final_Experian.dta", nogen keep(match) 

xtset id_borrowerlender year
eststo clear

local macro_ctrls DK_real_gdp_grate DK_gdp_growth_forecast DK_inflation_rate  vix
local fundingvars TotalEquity ShortTermDebt LongTermDebt LTfunding  

foreach LHS of local fundingvars { 
 
	eststo `LHS': qui reghdfe lndebt  /// 
		cL.(JK_mpshocksign `macro_ctrls')##cL.`LHS'_ratio ///
		if nonbank_lender == 1, noconstant ///
		a(id_borrower#year id_lender) vce(cluster id_lender#id_borrower)
		
}	 


esttab *Equity* *ShortTerm* *LongTerm* *LTfunding* using "$tables/mechanism_interactions.tex", ///
		prehead("{\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi} \begin{tabular}{l*{5}{c}} \toprule") star(* 0.10 ** 0.05 *** 0.01) ///
		posthead("\hline \\ \multicolumn{5}{l}{\textbf{A. Corporate credit}} \\\\[-1ex]") ///
		fragment ///
		rename(cL.JK_mpshocksign#cL.ShortTermDebt_ratio cL.JK_mpshocksign#cL.funding_ratio ///
		cL.JK_mpshocksign#cL.LongTermDebt_ratio cL.JK_mpshocksign#cL.funding_ratio ///
		cL.JK_mpshocksign#cL.LTfunding_ratio cL.JK_mpshocksign#cL.funding_ratio ///		
		cL.JK_mpshocksign#cL.TotalEquity_ratio cL.JK_mpshocksign#cL.funding_ratio) ///
		keep(cL.JK_mpshocksign#cL.funding_ratio)  /// 
		transform((exp(@)-1)*100 100*exp(@)) collabels(none)  varwidth(25) ///
		mtitles("Equity/TA" "STdebt/TA" "LTdebt/TA" "LT funding/TA") ///
		cells(b(star fmt(2)) se(fmt(2) par)) ///
		varlabels(cL.JK_mpshocksign#cL.funding_ratio "MP Shock x Funding ratio" ) ///
		stats(N r2, labels("Observations" "R2") fmt(%12.0gc 2)) booktabs replace
		


/////////////////////////////////////////////////////////////////////////////////
/// Table 7.b: Nonbank household lending and funding structure 
/////////////////////////////////////////////////////////////////////////////////


// Load household credit data FOR NONBANKS ONLY, and merge with lenders' balance sheets

use if delinquency != 1 & nonbank_lender == 1 using "$procdata/urtepers_panel_ALL.dta", clear 
rename cvrnr cvrnr_lender
merge m:1 cvrnr_lender year using "$procdata/final_Experian.dta", nogen keep(match) 


xtset id_borrowerlender year
eststo clear

local fundingvars TotalEquity ShortTermDebt LongTermDebt LTfunding  
local macro_ctrls DK_real_gdp_grate DK_gdp_growth_forecast DK_inflation_rate  vix
local fundingvars TotalEquity ShortTermDebt LongTermDebt LTfunding  

foreach LHS of local fundingvars { 
 
	eststo `LHS': qui reghdfe lndebt  /// 
		cL.(JK_mpshocksign `macro_ctrls')##cL.`LHS'_ratio ///
		if nonbank_lender == 1, noconstant ///
		a(id_borrower#year id_lender) vce(cluster id_lender#id_borrower)
		
}	 


qui estadd local MacroControlsInt "Yes": *
qui estadd local LowerInt "Yes": *
qui estadd local BorrowerYearFE "Yes": *
qui estadd local LenderFE "Yes": *

local stats_TestFE  N r2 MacroControlsInt LowerInt LenderFE BorrowerYearFE 
local label_TestFE `""Observations" "R2" "Macro Var. Interactions" "Lower level Interactions" "Lender FE"  "Borrower-Year FE" "'
	
esttab *Equity* *ShortTerm* *LongTerm* *LTfunding* using "$tables/mechanism_interactions.tex", ///
		prehead("{\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi} \begin{tabular}{l*{5}{c}} \toprule") ///
		star(* 0.10 ** 0.05 *** 0.01) fragment append nomtitles nonumbers nolines ///
		posthead("\hline \\ \multicolumn{5}{l}{\textbf{B. Consumer credit}} \\\\[-1ex]") ///
		rename(cL.JK_mpshocksign#cL.ShortTermDebt_ratio cL.JK_mpshocksign#cL.funding_ratio ///
		cL.JK_mpshocksign#cL.LongTermDebt_ratio cL.JK_mpshocksign#cL.funding_ratio ///
		cL.JK_mpshocksign#cL.LTfunding_ratio cL.JK_mpshocksign#cL.funding_ratio ///
		cL.JK_mpshocksign#cL.TotalEquity_ratio cL.JK_mpshocksign#cL.funding_ratio) ///
		keep(cL.JK_mpshocksign#cL.funding_ratio) ///
		transform((exp(@)-1)*100 100*exp(@)) collabels(none)  varwidth(25) ///
		cells(b(star fmt(2)) se(fmt(2) par)) ///
		varlabels(cL.JK_mpshocksign#cL.funding_ratio "MP Shock x Funding ratio" ) ///
		stats(`stats_TestFE', labels(`label_TestFE') fmt(%9.0fc 2)) 
		
